<?php

require_once("ui.php");
require_once("report.template.php");

class GeneralTab {
		
	private function printDebuggingTimeEdit() {
		$previousValue = getOriginalPost(INPUT_DEBUGGING_TIME);
		$previousValue = ($previousValue == null) ? "" : $previousValue;
		 
		echo '<input type="text" maxlength="20" value="' . $previousValue . '" name="' .
			INPUT_DEBUGGING_TIME . '" class="inlineTextInput"/>';
	} 

	private function printDefaultlyBlankSelect($id, $items) {
		$previousValue = getOriginalPost($id);		
		$optionsInfo = array();
		$defaultOptionValue;
		
		if (($previousValue == null) || ($previousValue == -1)) {			
			$optionInfo['value'] = '-1';
			$optionInfo['description'] = "";
			
			array_push($optionsInfo, $optionInfo);
			$defaultOptionValue = $optionInfo['value'];
		} else {
			$defaultOptionValue = $previousValue;
		}
		
		foreach ($items as $i => $item) {
			$optionInfo['value'] = $item['Id'];
			$optionInfo['description'] = $item['Name'];
			
			array_push($optionsInfo, $optionInfo);				
		}								
	
		printSelect($id, null, null, $optionsInfo, $defaultOptionValue, "");
	}
	
	private function printCheckboxes() {
		printPostCheckbox(INPUT_MISSING_DETECTION, "The way of detection of the problem is missing");
		printPostCheckbox(INPUT_MISSING_ROOT_CAUSE, "The root cause is missing");
		printPostCheckbox(INPUT_MISSING_METHOD, "The used method is missing");
		printPostCheckbox(INPUT_MISSING_TOOL, "The used tool is missing");
	}
	
	private function printProblemArea() {
		
		$previousValue = getOriginalPost(INPUT_PROBLEM_TEXT_AREA);
		$previousValue = ($previousValue == null) ? "" : $previousValue;
		
		echo '<textarea rows="2" cols="20" id="' . INPUT_PROBLEM_TEXT_AREA . '"
			name="' . INPUT_PROBLEM_TEXT_AREA . '">' . $previousValue . '</textarea>';
	}
	
	public function printTabContent() {
		
		global $db;		
		$developmentTime = "?";
		
		if(isUserLoggedIn()) {
			// user should be always logged in here
			
			global $loggedInUser;
			$userId = $loggedInUser->user_id;

			$sql = "SELECT Development_Time FROM `userCake_Users` WHERE User_ID = $userId";
			$db->sql_query($sql);
			$sqlItem = $db->sql_fetchrow();
			
			$developmentTime = $sqlItem['Development_Time'];
		}

		$sql = "SELECT Id, Name FROM `root_Cause_Complexities`";
		$db->sql_query($sql);
		$complexities = $db->sql_fetchall();
		
		$sql = "SELECT Id, Name FROM `feelings`";
		$db->sql_query($sql);
		$feelings = $db->sql_fetchall();

		global $loggedInUser;
?>
				<div class="reportTab" id="generalTab">
					<table>
						<tbody>
							<tr>
							
								<td>Project development time </td>
								<td>
									<?php echo convertDbTimeToString($developmentTime); ?>
									<a href="user.data.php">Update here</a>
									<div class="help" onmouseover="popup('Recommended to be updated once per work day. Consider the time for the whole work - including documenting, discussing and so on.');"></div>
								</td>
							</tr>
							<tr>
								<td>Debugging time for the reported bug</td>
								<td>
									<?php $this->printDebuggingTimeEdit(); ?>
									(e.g. 6h; 2h 30m; 50m; 0m; ...)
									<div class="help" onmouseover="popup('Strict measuring of the time may not be very practical. Therefore, try at least to note the time when you start debugging.');"></div>
								</td>
							</tr>
							<tr>
								<td>Complexity of the debugged problem</td>
								<td>
								
								<?php
									$this->printDefaultlyBlankSelect(INPUT_DEBUGGING_COMPLEXITIES, $complexities);
								?>
									<div class="help" onmouseover="popup('Amount of thinking that is needed to understand the situation and for the analysis of the problem. \
										It does not necessarily have to reflect the debugging time. <br><br>\
										Examples:<br> \
										<b>Trivial</b> - checking code that implements a straight-forward idea, no large pointer machinations, no complex conditions<br> \
										<b>Easy</b> - requires some thinking, insertion to a link list<br> \
										<b>Medium</b> - making charts or notes starts to be useful, insertion to an AVL tree<br> \
										<b>Hard</b> - requires an analysis, the programmer thinks a lot, searching for a race condition that cannot be reproduced easily',
											440);"></div>
									<br>
									<b>Ignore debugging time.</b>
								</td>
							</tr>
							<tr>
								<td>Feelings</td>
								<td>
								<?php
									$this->printDefaultlyBlankSelect(INPUT_FEELINGS, $feelings);
								?>
									<div class="help" onmouseover="popup('Suppose that today is some another day in the future. You arrive to your work, do some programming for an hour or two, exchange a few interesting news with your colleagues and then you encounter the similiar problem like just now. How would you feel?', 400);"></div>
									<br>
									<b>After one year when you encounter the same kind of problem.</b>
								</td>
							</tr>
							<tr>
								<td>
									Troubles:<br>
									<?php
										$this->printCheckboxes(); 
									?>
								</td>
								<td>Feel free to describe the reason for reviewing or fill in the missing things.<br>
									<?php
										$this->printProblemArea();
									?>
								</td>
							</tr>
						</tbody>
					</table>
				</div>
<?php 	
	}
}
?>